package com.qf.service.impl;/*
 *保护你的大熊
 *2021/11/29
 *19:49
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    DtsCategoryMapper categoryMapper;

    /**
     * 二级分类id
     * @param cateId
     * @return
     */
    @Override
    public List<Integer> findCategoryId(Integer cateId) {

        DtsCategory category = categoryMapper.selectById(cateId);

        Integer pid = category.getPid();

        List<Integer> ids = new ArrayList<>();
        ids.add(pid);
        ids.add(cateId);

        return ids;
    }

    @Override
    public List<CatVo> findCategoryList() {

        //1.首先查询一级分类列表
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getPid,0);

        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);

        if (categoryList != null) {
            List<CatVo> catVoList = categoryList.stream().map(category -> {
                //一个分类对象  对应一个 CatVo对象
                CatVo catVo = new CatVo();
                catVo.setValue(category.getId());
                catVo.setLabel(category.getName());

                //查询二级分类列表
                LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DtsCategory::getDeleted,false);
                wrapper1.eq(DtsCategory::getPid,category.getId());
                List<DtsCategory> categories = categoryMapper.selectList(wrapper1);

                if (categories != null) {
                    List<CatVo> catVoList1 = categories.stream().map(category1 -> {
                        //一个分类对象  对应一个 CatVo对象
                        CatVo catVo1 = new CatVo();
                        catVo1.setValue(category1.getId());
                        catVo1.setLabel(category1.getName());
                        return catVo1;
                    }).collect(Collectors.toList());

                    //设置  二级分类列表
                    catVo.setChildren(catVoList1);
                }

                return catVo;
            }).collect(Collectors.toList());

            return catVoList;
        }

        return null;
    }

    @Override
    public boolean isExists(Integer categoryId) {

        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getId,categoryId);

        int count = categoryMapper.selectCount(wrapper);

        return count != 0;

//        if(count == 0){
//            return false;
//        }
//
//        return true;
    }
}
